package algotrader.gui;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import algotrader.OperadorPregao;
import algotrader.book.BookManager;
 
public class OperacaoPanel extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	private BookPanel bookPanel;
    private javax.swing.JLabel labelSimbolo;
    private OrderSendingPanel orderSendingPanel;
    private OrdersPanel ordersPanel;
    private javax.swing.JTextField simbolo;
    private javax.swing.JPanel simboloPanel;


    public OperacaoPanel(BookManager bookManager,OperadorPregao operadorPregao) {
    	bookPanel = new BookPanel(bookManager);
        ordersPanel = new OrdersPanel(operadorPregao);
        orderSendingPanel = new OrderSendingPanel(operadorPregao);
        initComponents();
    }

    private void initComponents() {

        simboloPanel = new javax.swing.JPanel();
        labelSimbolo = new javax.swing.JLabel();
        simbolo = new javax.swing.JTextField();
        
        simbolo.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
               	 System.out.println("requisitando book...");
                    requisitaBook(simbolo.getText());
                }
            }
        });

        setMaximumSize(null);
        setMinimumSize(null);

        simboloPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Escolha o simbolo"));

        labelSimbolo.setText("Simbolo:");

        javax.swing.GroupLayout simboloPanelLayout = new javax.swing.GroupLayout(simboloPanel);
        simboloPanel.setLayout(simboloPanelLayout);
        simboloPanelLayout.setHorizontalGroup(
            simboloPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(simboloPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelSimbolo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(simbolo, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(398, Short.MAX_VALUE))
        );
        simboloPanelLayout.setVerticalGroup(
            simboloPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(simboloPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(simboloPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSimbolo)
                    .addComponent(simbolo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bookPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
                    .addComponent(simboloPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ordersPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
                    .addComponent(orderSendingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(simboloPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bookPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(orderSendingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ordersPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }

	protected void requisitaBook(String text) {
		bookPanel.requisitaBook(text);
		ordersPanel.alteraAtivo(text);
		orderSendingPanel.alteraAtivo(text);
	}
}